package com.balance.anole.erp.report.action;

import org.eclipse.jface.action.Action;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.printing.PrintDialog;
import org.eclipse.swt.printing.Printer;
import org.eclipse.swt.printing.PrinterData;
import org.eclipse.swt.widgets.Display;

import com.balance.anole.erp.constant.IIcons;

public class ReportDemoPrintAction extends Action{
	public void setEnabled(boolean enabled){
		super.setEnabled(enabled);
		if(enabled){
			setImageDescriptor(com.balance.anole.erp.Activator.getImageDescriptor(IIcons.PRINT));		
		}else{
			setImageDescriptor(com.balance.anole.erp.Activator.getImageDescriptor(IIcons.DISABLE_PRINT));
		}
	}
	
	public ReportDemoPrintAction(){
		setToolTipText("Print");		
	}
	
	public void run(){
		PrintDialog dialog = new PrintDialog(Display.getCurrent().getActiveShell(), SWT.NONE);
		PrinterData data = dialog.open();
        if (data == null) return;
        if (data.printToFile) {
            data.fileName = "print.out"; 
        }
        Printer printer = new Printer(data);
        print(printer);
        printer.dispose();
	}
	
	private void print(Printer printer) {
        if (printer.startJob("Anole Print")) {   // the string is the job name - shows up in the printer's job list
            Rectangle trim = printer.computeTrim(0, 0, 0, 0);
            Point screenDPI = Display.getCurrent().getActiveShell().getDisplay().getDPI();
            Point printerDPI = printer.getDPI();
            int scaleFactor = printerDPI.x / screenDPI.x;
            //int width = image.getImageData().width;
            //int height = image.getImageData().height;
            /**//* Create printer GC, and create and set the printer font */ 
            GC gc = new GC(printer);
            gc.drawText("some text here", scaleFactor * 20, scaleFactor * 20);
            
            printer.endJob();
            gc.dispose();
  
        }
    }
}
